package org.eae.eldf.webapp.controller.base;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;
import org.eae.eldf.dto.base.BaseCompanyInfoQueryDto;
import org.eae.eldf.entity.base.BaseCompanyInfo;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.service.base.BaseCompanyInfoService;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;

@Controller
@RequestMapping("/base")
public class BaseCompanyInfoController extends AbstractController<BaseCompanyInfo> {
	private BaseCompanyInfoService baseCompanyInfoService;

	@Autowired
	public void setBaseCompanyInfoService(BaseCompanyInfoService baseCompanyInfoService) {
		this.baseCompanyInfoService = baseCompanyInfoService;
	}

	/**
	 * 企业列表
	 * 
	 * @param map
	 * @param searchValue
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/company/list")
	public String listPage(ModelMap map, @RequestParam(required = false) String searchValue, HttpServletRequest request,
			HttpServletResponse response) {
		/*
		 * Pagination<BaseCompanyInfo> pagerPagination=this.initPager(request,
		 * response);
		 * pagerPagination=baseCompanyInfoService.queryListBaseCompanyInfo(
		 * pagerPagination, searchValue);
		 * map.addAttribute("list",pagerPagination);
		 * map.addAttribute("searchValue", searchValue);
		 */
		return "/base/company/base_company";
	}

	@RequestMapping(value = "/company/companylist")
	public @ResponseBody ResultList<BaseCompanyInfo> pageList(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "baseCompanyInfoQueryDto") BaseCompanyInfoQueryDto baseCompanyInfoQueryDto) {

		Pagination<BaseCompanyInfo> pager = super.newPaginationInstance(baseCompanyInfoQueryDto);
		pager = baseCompanyInfoService.queryListBaseCompanyInfo(pager, baseCompanyInfoQueryDto);

		return outPageArrayList(pager, baseCompanyInfoQueryDto);
	}
	
	
	@RequestMapping(value = "/company/companyAlllist")
	public @ResponseBody List<BaseCompanyInfo> pageAllList(HttpServletRequest request, HttpServletResponse response) {

		return baseCompanyInfoService.queryAll();
	}
	
	
	//reids加载企业信息
	@RequestMapping(value = "/company/loadcompanytoredis")
	public @ResponseBody Result loadCompanyToRedis(HttpServletRequest request, HttpServletResponse response){
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		try {
			baseCompanyInfoService.loadCompanyToRedis();
		} catch (Exception e) {
			result.setResultValue(false);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		
		return result;
	}

	/**
	 * 新增企业
	 * 
	 * @Date : 2016年10月25日
	 * @param request
	 * @param response
	 * @param baseCompanyInfo
	 * @return
	 */

	@RequestMapping(value = "/company/add")
	@ResponseBody
	public Result insert(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "baseCompanyInfo") BaseCompanyInfo baseCompanyInfo) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		if (baseCompanyInfoService.queryCount(Criteria.create(BaseCompanyInfo.class).where("companyCode",
				new Object[] { baseCompanyInfo.getCompanyCode() })) > 0) {
			RequestContext context = new RequestContext(request);
			result.setResultValue(false);
			result.setMessage(context.getMessage("company.msg.companycode.exist"));
		} else {
//			Criteria c =	Criteria.create(BaseCompanyInfo.class).set("companyCode", baseCompanyInfo.getCompanyCode())
//			.set("companyName", baseCompanyInfo.getCompanyName())
//			.set("companyNameKey", baseCompanyInfo.getCompanyNameKey())
//			.set("companyAddr", baseCompanyInfo.getCompanyAddr())
//			.set("companyRemark", baseCompanyInfo.getCompanyRemark())
//			.set("companyScale", baseCompanyInfo.getCompanyScale())
//			.set("isEnable", baseCompanyInfo.getIsEnable())
//			.set("createUser", SpringSecurityUtils.getCurrentUserName());
//			
			baseCompanyInfoService.createCompany(baseCompanyInfo);
		}
		return result;
	}

	/**
	 * 新增企业
	 * 
	 * @Date : 2016年10月25日
	 * @param request
	 * @param response
	 * @param baseCompanyInfo
	 * @return
	 */

	@RequestMapping(value = "/company/update")
	@ResponseBody
	public Result update(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "baseCompanyInfo") BaseCompanyInfo baseCompanyInfo) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		try {
			baseCompanyInfoService.updateCompany(baseCompanyInfo);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return result;
	}

	
	
	 /**
     *  删除企业
     */
    @RequestMapping(value="/company/del")
    public @ResponseBody Result delete(@RequestParam Long id){
    	Result result = new Result(StateDefine.FLAG_SUCCESS, true);
    	try {
    		baseCompanyInfoService.delete(BaseCompanyInfo.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return result;
    }

	/**
	 * 清除该企业的所有redis缓存
	 * 
	 * @Date : 2016-8-2
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/company/clearRedis")
	public @ResponseBody Result clearRedis(@RequestParam String companyCode) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		try {
			baseCompanyInfoService.clearRedis(companyCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
